-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add import
support to ssrModuleLoader
#5197
Conversation
Just wanted to chime in to give a +1 to this approach as Snowpack creator/maintainer: we landed this support in Snowpack several months ago and have had no issues that I'm aware of. |
Thanks for this PR @natemoo-re, we'll discuss it in this week's team meeting. |
Just leaving a note here to reflect discussion from Discord
The |
import
support to ssrModuleLoader
import
support to ssrModuleLoader
Updated to use |
Ah, just figured out why the test suite is failing! Will take some time to see if I can get things running again locally.
Edit Welp, |
d2d1ee1
to
3b6768c
Compare
@natemoo-re I also ran into the segfault issue with Jest. See #5213 (comment). I wasn't able to solve it as I think it's something with Node or Jest that causes this... |
d2a0d48
to
2c70d99
Compare
Here's a PR with Astro's test suite passing with this change: withastro/astro#1585 |
I've also tested this branch locally at sveltejs/vite-plugin-svelte#204, and the changes here are working as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for testing this in Astro and Svelte @matthewp @bluwy. We discussed with @sodatea, and we think that with these external tests available it is safe to merge this PR as part of the next minor as we will also get extra testing from the beta period. Merging this one now in preparations for this release
Description
This PR adds SSR support for loading Node ESM packages using
import
rather than always usingrequire
. This improves compatibility with the growing number of packages that are shipped as ESM only.Additional context
This is implemented using an approach that Snowpack has been using with great success.The basic steps are:1. Attempt torequire
the package2. Handle any errors that may have happened during therequire
- If it's aSyntaxError
and we see animport
orexport
keyword, the user is likely using an outdated Node version. Give them a helpful error.- If it's the knownERR_REQUIRE_ESM
error, attempt toimport
the module.- If it's any other error, surface it with anotherthrow
.This PR has been updated to load everything (CJS and ESM) via
import()
rather thanrequire
with animport()
fallback.One caveat to this PR: TypeScript currently doesn't have a way to not compile dynamic
import()
, so we need to use thenative-import.js
file to load it. Whentypescript@4.5.0
is out of Beta and is officially released, this can be cleaned up significantly.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).